var express = require('express');
var router = express.Router();
var retUtil = require('../../../../utils/retUtil');
var api4back = require('../../../../service/api4back');
var CONST = require('../../../../utils/const');


/**
 * 获取推广方信息
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：device
 */
function getPromoterInfo(req, res, next) {
    //获取参数
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    //参数判断
    if(!userId){
        return retUtil.ret_err_json("用户唯一标识无效",res);
    }
    //数据请求
    var para = {
        body :{
            userId:userId
        },
        method : "POST"
    };
    api4back.getPromoterInfo(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getPromoterInfo', getPromoterInfo);
router.post('/getPromoterInfo', getPromoterInfo);



/**
 * 获取手机号验证码
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：mobile : 用户手机号, 验证规则可放宽, 仅限制1开头, 11位纯数字即可
 * 参数：zone : 国际区号, 暂时支持国内, 固定传 +86
 * 参数：type : 验证码类型, 暂定如下:1 短信验证码
                                 2 语音验证码
                                 暂时固定为1
 */
function getBackMobileVerCode(req, res, next) {
    //获取参数
    var mobile = retUtil.numberFromReq(req, 'mobile') || 0;
    var zone = "+86";
    var type = 1;
    //参数判断
    if(!mobile){
        return retUtil.ret_err_json("手机号有误",res);
    }

    // 避免mobile被连续发送验证码，检测session
    var quickly = false;
    if(req.session.hasOwnProperty('code_mobile')&&
        req.session.code_mobile &&
        //同一个手机号做判断
        req.session.code_mobile==mobile &&
        req.session.hasOwnProperty('code_create_time')&&
        req.session.code_create_time){
        var cur_time = Math.floor(Date.now()/1000);
        if(cur_time-req.session.code_create_time<30){
            quickly = true;
        }

    }
    if(quickly){
        return retUtil.ret_err_json('请勿频繁发送',res);
    }

    //数据请求
    var para = {
        body :{
            mobile:mobile,
            zone:zone,
            type:type
        },
        method : "POST"
    };
    api4back.getBackMobileVerCode(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        req.session.code_mobile = mobile;
        req.session.code_create_time = Math.floor(Date.now()/1000);
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getBackMobileVerCode', getBackMobileVerCode);
router.post('/getBackMobileVerCode', getBackMobileVerCode);




/**
 * 验证验证码
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：mobile : 用户手机号, 验证规则可放宽, 仅限制1开头, 11位纯数字即可
 * 参数：zone : 国际区号, 暂时支持国内, 固定传 +86
 * 参数：verCode : 六位验证码
 2 语音验证码
 暂时固定为1
 */
function checkBalkMobileCode(req, res, next) {
    //获取参数
    var mobile = retUtil.numberFromReq(req, 'mobile') || 0;
    var zone = "+86";
    var verCode = retUtil.stringFromReq(req, 'captcha') || "";
    //参数判断
    if(!mobile && !verCode){
        return retUtil.ret_err_json("手机号或验证码有误",res);
    }
    //数据请求
    var para = {
        body :{
            mobile:mobile,
            zone:zone,
            verCode:verCode
        },
        method : "POST"
    };
    api4back.checkBalkMobileCode(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        req.session[CONST.SESSION_USER] = apiRes;
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/checkBalkMobileCode', checkBalkMobileCode);
router.post('/checkBalkMobileCode', checkBalkMobileCode);



/**
 * 获取用户详细信息
 * @author 李强
 * @date 2018-07-3
 */
function getUserInfo(req, res, next) {
    var userInfo = req.session[CONST.SESSION_USER];
    if(!userInfo){
        return retUtil.ret_err_json("获取失败", res);
    }
    userInfo.menu = [];
    if(userInfo.orgInfo[0].orgType == 400){
        //网红公司
        userInfo.menu = CONST.COMPANY_MENU;
    }else if(userInfo.orgInfo[0].orgType == 410){
        //推广码推广方
        userInfo.menu = CONST.PROMOTER_MENU;
    }else{
        userInfo.menu = CONST.EXCEPTION_MENU;
    }
    return retUtil.ret_succ_json(userInfo,res);
}
router.get('/getUserInfo', getUserInfo);
router.post('/getUserInfo', getUserInfo);


/**
 * 获取用户侧边栏数据
 * @author 李强
 * @date 2018-07-10
 */
function getMenuData(req, res, next) {
    var userInfo = req.session[CONST.SESSION_USER];
    if(!userInfo){
        return retUtil.ret_err_json("获取失败", res);
    }
    var menu = [];
    if(userInfo.orgInfo[0].orgType == 400){
        //网红公司
        menu = CONST.COMPANY_MENU;
    }else if(userInfo.orgInfo[0].orgType == 410){
        //推广码推广方
        menu = CONST.PROMOTER_MENU;
    }else{
        menu = CONST.EXCEPTION_MENU;
    }

    return retUtil.ret_succ_json(menu,res);
}
router.get('/getMenuData', getMenuData);
router.post('/getMenuData', getMenuData);



/**
 * 删除用户session中的信息
 * @author 李强
 * @date 2018-07-6
 */
function deleteUserInfo(req, res, next) {
    req.session[CONST.SESSION_USER] = null;
    return retUtil.ret_succ_json({},res);
}
router.get('/deleteUserInfo', deleteUserInfo);
router.post('/deleteUserInfo', deleteUserInfo);




module.exports = router;
